/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package semanticsearch.util.sql;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Pavithre
 */
public class MySqlConnection {
    
    Connection conn;
    
    public Connection openConnection(){
        
        System.out.println("MySQL Connect Example.");
        conn = null;
        String url = "jdbc:mysql://localhost:3306/";
        String dbName = "search";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "root";
        String password = "";
        
        try {
            
            Class.forName(driver).newInstance();
            conn = (Connection) DriverManager.getConnection(url + dbName, userName, password);
            System.out.println("Connected to the database");
            return conn;
            
        }catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e) {
            e.printStackTrace();
        }
        
        return null;
    }
    
    public void closeConnection(){
        
        try {
            
            conn.close();
            System.out.println("Connection Closed.");
            
        } catch (SQLException ex) {
            Logger.getLogger(MySqlConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
//    public static void main(String[] args) {
//        
//        MySqlConnection m = new MySqlConnection();
//        m.openConnection();
//        m.closeConnection();
//    } 
}
